// =============================================================================
// Gulp Task Dependencies
// =============================================================================

var gulp = require('gulp');
var browserSync = require('browser-sync'); //local service
var reload = browserSync.create().reload;

var clean = require('gulp-clean'); //del file
var rev = require('gulp-rev'); //update version number , md5
var revCollector = require('gulp-rev-collector'); // deprecated

var wiredep = require('wiredep').stream;

/** update lib reference in HTML */
var inject = require('gulp-inject');
var series = require('stream-series');
var htmlReplace = require('gulp-html-replace');

/** Compress HTML */
var htmlmin = require('gulp-htmlmin');
var gulpRemoveHtml = require('gulp-remove-html');
var removeEmptyLines = require('gulp-remove-empty-lines');

/** Compress CSS */
var minify = require('gulp-minify-css'); //deprecated
var cleanCSS = require('gulp-clean-css'); //compress CSS

var eslint = require('gulp-eslint'); //check js
var jshint = require('gulp-jshint');

/** Compress JS */
var uglify = require('gulp-uglify'); //compress&ugly js

var rename = require('gulp-rename'); //rename files' name
var concat = require('gulp-concat'); //merge multi files

/** Compress Image */
var imagemin = require('gulp-imagemin'); //compress image

var runSequence = require('run-sequence');
// var gulpSequence = require('gulp-sequence');

/* 解决AngularJS 缩写写法 gulp打包压缩后JS报错问题 */
var ngAnnotate = require('gulp-ng-annotate');


// =============================================================================
// Gulp Task
// =============================================================================

var buildBasePath = 'build'; //build ouput files folder

// =======================================================================
// Local -- Preview Task, No Compress, No Package, No Mix...
// =======================================================================

gulp.task('cleanTemp', function() {
    return gulp.src('./Temp', { read: false })
        .pipe(clean());
});

/** Start Local Server */
gulp.task('start:local', function() {
    browserSync({
        open: true,
        port: '9000',
        // server: {
        //     baseDir: "./Temp",
        //     index: 'index.html'
        // },
        proxy: 'http://localhost/sites/JDBToolsPlatform/ProcessQualityReport/Temp',
        https: false,
        // serveStatic: [{
        //     route: '/index.html',
        //     dir: ['./Temp', './app']
        // }]
        // https: {
        //     key: "path-to-custom.key",
        //     cert: "path-to-custom.crt"
        // }
    });

    gulp.watch('./app/**/*', ['copy:temp']);
    //gulp.watch('./Temp/*.html').on('change', reload);
});

/** Copy Original Files To Temp */
gulp.task('copy:temp', function() {
    return gulp.src('./app/**/*')
        .pipe(gulp.dest('./Temp'))
        .pipe(browserSync.reload({ stream: true })); //所有文件都会刷新;Todo
});

/** 将bower依赖自动注入HTML文件中 */
gulp.task('bowerInject', function() {
    return gulp.src('./app/index.html')
        .pipe(wiredep({
            optional: 'configuration',
            goes: 'here'
        }))
        .pipe(gulp.dest('./tmp'));
});

/** 本地测试组合任务 -- 无压缩、带注释 */
gulp.task('local', function() {
    runSequence('cleanTemp', 'copy:temp', 'start:local');
});


// =======================================================================
// Prod -- Local Preview Task, Compressed, Mixed, md5 ...
// =======================================================================


/** Clean The Whole build Folder */
gulp.task('cleanBuild', function() {
    return gulp.src(buildBasePath, { read: false })
        .pipe(clean());
});

/** Copy Vendor Lib Files To Prod */
gulp.task('copyVendor2prod', function() {
    return gulp.src('./app/vendor/**/*')
        .pipe(gulp.dest(buildBasePath + '/vendor'));
});

gulp.task('copyFavicon', function() {
    return gulp.src('./app/favicon.ico')
        .pipe(gulp.dest(buildBasePath));
});

/** 压缩HTML(先合并HTML中CSS/JS)*/
gulp.task('minifyHtml', function() {
    var options = {
        removeComments: true, //清除HTML注释
        collapseWhitespace: true, //压缩HTML为一行
        collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input />
        removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true, //删除<style>和<link>的type="text/css"
        minifyJS: true, //压缩页面JS
        minifyCSS: true //压缩页面CSS
    };
    return gulp.src(buildBasePath + '/**/*.html')
        .pipe(gulpRemoveHtml()) //清除特定标签
        .pipe(removeEmptyLines())
        .pipe(removeEmptyLines({ removeComments: true })) //清除注释
        .pipe(htmlmin(options))
        .pipe(gulp.dest(buildBasePath))
        .pipe(browserSync.reload({ stream: true }));
});

/** 压缩CSS */
gulp.task('minifyCss', function() {
    return gulp.src('./app/assets/css/*.css')
        //.pipe(rename({ suffix: '.min' }))
        .pipe(cleanCSS({ debug: true }, function(details) {
            console.log(details.name + ': ' + details.stats.originalSize);
            console.log(details.name + ': ' + details.stats.minifiedSize);
        }))
        .pipe(concat('bundle.min.css')) //合并为一个文件
        //.pipe(rev()) //添加md5
        .pipe(gulp.dest(buildBasePath + '/assets/css'))
        .pipe(browserSync.reload({ stream: true }));
});

/** 压缩JS */
gulp.task('minifyjsmd5', function() {
    return gulp.src('./app/assets/**/*.js')
        //.pipe(concat('bundle.min.js')) //合并压缩后的js为一个文件
        //.pipe(uglify()) //压缩js到一行
        //.pipe(rev()) //文件名加MD5后缀
        .pipe(gulp.dest(buildBasePath + '/assets/'))
        .pipe(browserSync.reload({ stream: true })); //输出到js目录
});

gulp.task('minifyJS', function() {
    return gulp.src('./app/assets/js/**/*.js')
        .pipe(ngAnnotate())
        .pipe(concat('bundle.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest(buildBasePath + '/assets/js'));
});

/** 压缩图片 */
gulp.task('minifyImg', function() {
    return gulp.src('./app/assets/images/*')
        .pipe(imagemin())
        .pipe(gulp.dest(buildBasePath + '/assets/images'))
        .pipe(browserSync.reload({ stream: true }));
});

/** Start Build Server Preview */
gulp.task('start:build', function() {
    browserSync({
        open: true,
        port: '9001',
        proxy: 'http://localhost/sites/JDBToolsPlatform/ProcessQualityReport/' + buildBasePath,
        https: false,
    });
});

/**
 * 清除HTML中所有CSS JS引用
 * <!-- build:css -->
 *  ......
 * <!-- endbuild -->
 * <!-- build:js -->
 *  ......
 * <!-- endbuild -->
 */

//var autoInjectHTMLFiles = ['app/index.html', 'app/editor.html', 'app/reportdetails.html'];
gulp.task('autoInject', function() {
    return gulp.src('app/**/*.html')
        .pipe(htmlReplace({
            'css': './assets/css/bundle.min.css',
            'js': './assets/js/bundle.min.js'
        }))
        .pipe(gulp.dest(buildBasePath));
});

gulp.task('prod', function() {
    runSequence('cleanBuild', 'copyVendor2prod', 'copyFavicon', 'minifyJS', 'minifyCss', 'minifyImg', 'autoInject', 'minifyHtml', 'start:build');
});